﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace B4Deal.Models
{
    public class Deal
    {
        private int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }

        private int maKM;

        public int MaKM
        {
            get { return maKM; }
            set { maKM = value; }
        }
        private int loaiKM = 0;

        public int LoaiKM
        {
            get { return loaiKM; }
            set { loaiKM = value; }
        }
        private string tenKM = String.Empty;

        public string TenKM
        {
            get { return tenKM; }
            set { tenKM = value; }
        }
        private int donGiaCu = 0;

        public int DonGiaCu
        {
            get { return donGiaCu; }
            set { donGiaCu = value; }
        }
        private int donGiaMoi = 0;

        public int DonGiaMoi
        {
            get { return donGiaMoi; }
            set { donGiaMoi = value; }
        }

        private DateTime batDauKM = new DateTime(2012, 1, 1);
        public DateTime BatDauKM
        {
            get { return batDauKM; }
            set { batDauKM = value; }
        }
        private DateTime ketThucKM = new DateTime(2012, 1, 1);

        public DateTime KetThucKM
        {
            get { return ketThucKM; }
            set { ketThucKM = value; }
        }
        private string moTa = String.Empty;

        public string MoTa
        {
            get { return moTa; }
            set { moTa = value; }
        }
        private string dacDiemNoiBat = String.Empty;

        public string DacDiemNoiBat
        {
            get { return dacDiemNoiBat; }
            set { dacDiemNoiBat = value; }
        }
        private string dkSuDung = String.Empty;

        public string DkSuDung
        {
            get { return dkSuDung; }
            set { dkSuDung = value; }
        }
        private string gioiThieuChiTiet = String.Empty;

        public string GioiThieuChiTiet
        {
            get { return gioiThieuChiTiet; }
            set { gioiThieuChiTiet = value; }
        }
        private int maDN;

        public int MaDN
        {
            get { return maDN; }
            set { maDN = value; }
        }
        private string hinhAnh = String.Empty;

        public string HinhAnh
        {
            get { return hinhAnh; }
            set { hinhAnh = value; }
        }
        private string clip = String.Empty;

        public string Clip
        {
            get { return clip; }
            set { clip = value; }
        }
        private int tinhTrang = 0; //Haven't Pass by Admin

        public int TinhTrang
        {
            get { return tinhTrang; }
            set { tinhTrang = value; }
        }

        private int hinhThucGiaoSP = 0;

        public int HinhThucGiaoSP
        {
            get { return hinhThucGiaoSP; }
            set { hinhThucGiaoSP = value; }
        }

        private int nNguoi = 0;

        public int NNguoi
        {
            get { return nNguoi; }
            set { nNguoi = value; }
        }
        private int nVote = 0;

        public int NVote
        {
            get { return nVote; }
            set { nVote = value; }
        }

        public Deal()
        {
        }

        // string strResource = "../../Content/Layout/Images/deals/";
        public Deal(int iMaKM, int iLoaiKM, string itenKM, int iDonGiaCu, int iDonGiaMoi, DateTime iKetThucKM,
                    string iMota, string iNoiBat, string iDkSuDung, string iGioiThieu, int iMaDN,
                    string iHinhAnh, string iClip, int iHinhThuc, int iTinhTrang, int iSoNguoi, int iVote)
        {
            this.MaKM = iMaKM;
            this.LoaiKM = iLoaiKM;
            this.TenKM = itenKM;
            this.DonGiaCu = iDonGiaCu;
            this.DonGiaMoi = iDonGiaMoi;
            this.KetThucKM = iKetThucKM;
            this.MoTa = iMota;
            this.DacDiemNoiBat = iNoiBat;
            this.DkSuDung = iDkSuDung;
            this.GioiThieuChiTiet = iGioiThieu;
            this.MaDN = iMaDN;
            this.HinhAnh = iHinhAnh.Substring(0, iHinhAnh.LastIndexOf('.')) + "/1.jpg";
            this.Clip = iClip;
            this.HinhThucGiaoSP = iHinhThuc;
            this.TinhTrang = iTinhTrang;
            this.NNguoi = iSoNguoi;
            this.NVote = iVote;
        }
        public Deal(int iMaKM, int iLoaiKM, string itenKM, int iDonGiaCu, int iDonGiaMoi, DateTime iBatDauKM, DateTime iKetThucKM,
                  string iMota, string iNoiBat, string iDkSuDung, string iGioiThieu, int iMaDN,
                  string iHinhAnh, string iClip, int iHinhThuc, int iTinhTrang, int iSoNguoi, int iVote)
        {
            this.MaKM = iMaKM;
            this.LoaiKM = iLoaiKM;
            this.TenKM = itenKM;
            this.DonGiaCu = iDonGiaCu;
            this.DonGiaMoi = iDonGiaMoi;
            this.BatDauKM = iBatDauKM;
            this.KetThucKM = iKetThucKM;
            this.MoTa = iMota;
            this.DacDiemNoiBat = iNoiBat;
            this.DkSuDung = iDkSuDung;
            this.GioiThieuChiTiet = iGioiThieu;
            this.MaDN = iMaDN;
            this.HinhAnh = iHinhAnh; 
            this.Clip = iClip;
            this.HinhThucGiaoSP = iHinhThuc;
            this.TinhTrang = iTinhTrang;
            this.NNguoi = iSoNguoi;
            this.NVote = iVote;
        }
        public List<Deal> getAllDeal()
        {
            List<Deal> deals = new List<Deal>();
            using (var db = new B4Deal_DataEntities())
            {
                var temp = db.KHUYENMAIs.ToList();
                foreach (var deal in temp)
                    deals.Add(new Deal(deal.MaKM, Convert.ToInt32(deal.LoaiKM), deal.TenKM, Convert.ToInt32(deal.DonGiaCu), Convert.ToInt32(deal.DonGiaMoi), Convert.ToDateTime(deal.BatDauKM), Convert.ToDateTime(deal.KetThucKM),
                                       deal.MoTa, deal.DiemNoiBat, deal.DKSuDung, deal.GioiThieuChiTiet,
                                       Convert.ToInt32(deal.DoanhNghiep), deal.HinhAnh, deal.ClipQuangCao, Convert.ToInt32(deal.HinhThucGiaoSP), Convert.ToInt32(deal.TinhTrang), deal.DONHANGs.Count, 1000));
            }
            return deals;
        }

        public List<Deal> QueryDealByType(int type)
        {
            List<Deal> deals = new List<Deal>();
            using (var db = new B4Deal_DataEntities())
            {
                var temp = db.KHUYENMAIs.Where(d => d.LoaiKM == type).ToList();
                foreach (var deal in temp)
                    deals.Add(new Deal(deal.MaKM, Convert.ToInt32(deal.LoaiKM), deal.TenKM, Convert.ToInt32(deal.DonGiaCu), Convert.ToInt32(deal.DonGiaMoi), Convert.ToDateTime(deal.KetThucKM),
                                       deal.MoTa, deal.DiemNoiBat, deal.DKSuDung, deal.GioiThieuChiTiet,
                                       Convert.ToInt32(deal.DoanhNghiep), deal.HinhAnh, deal.ClipQuangCao, Convert.ToInt32(deal.HinhThucGiaoSP), Convert.ToInt32(deal.TinhTrang), deal.DONHANGs.Count, 1000));
            }
            return deals;
        }

        public List<Comment> QueryCommentByDealID()
        {
            List<Comment> comments = new List<Comment>();
            using (var db = new B4Deal_DataEntities())
            {
                var temp = db.COMMENTs.Where(c => c.KhuyenMai == this.MaKM).ToList();
                foreach (var item in temp)
                    comments.Add(new Comment(item.NoiDung, Convert.ToDateTime(item.ThoiGian), Convert.ToInt32(item.NguoiPost)));
            }
            return comments;
        }

        public Business QueryBusinessByDealID()
        {
            using (var db = new B4Deal_DataEntities())
            {
                var temp = db.DOANHNGHIEPs.Where(b => b.MaDN == this.MaDN).ToList()[0];
                return new Business(temp.DiaChi, temp.ThongTinHopTac);
            }
        }


        public static List<Deal> GetDeals(int i, int place)
        {
            using (var db = new B4Deal_DataEntities())
            {
                string strResource = "../../Content/Layout/Images/deals/";
                List<Deal> deals = new List<Deal>();
                var list = db.KHUYENMAIs.ToList();
                list.Sort((i1, i2) =>
               {
                   if (i1.KetThucKM >= i2.KetThucKM)
                       return 1;
                   else
                       return -1;
               });
                foreach (var deal in list)
                {
                    DateTime temp = Convert.ToDateTime(deal.KetThucKM);
                    if (deals.Count < i && temp >= DateTime.Today)
                        deals.Add(new Deal(deal.MaKM, Convert.ToInt32(deal.LoaiKM), deal.TenKM, Convert.ToInt32(deal.DonGiaCu), Convert.ToInt32(deal.DonGiaMoi), temp,
                                           deal.MoTa, deal.DiemNoiBat, deal.DKSuDung, deal.GioiThieuChiTiet,
                                           Convert.ToInt32(deal.DoanhNghiep), strResource + deal.HinhAnh, strResource + deal.ClipQuangCao, Convert.ToInt32(deal.HinhThucGiaoSP), Convert.ToInt32(deal.TinhTrang), deal.DONHANGs.Count, 1000));
                }
                return deals;
            }

        }

        public static string Url(string productname, string price, string returnUrl, string comment)
        {
            string url = "http://sandbox.nganluong.vn/button_payment.php?receiver=trongnghia09ctt2@gmail.com";
            url += "&product_name=" + productname;
            url += "&price=" + price;
            url += "&return_url=" + returnUrl;
            url += "&comments=" + comment;
            return url;
        }

        public bool SaveDeal()
        {
            using (var db = new B4Deal_DataEntities())
            {
                try
                {
                    KHUYENMAI km = new KHUYENMAI();
                    km.MaKM = maKM;
                    km.LoaiKM = loaiKM;
                    km.TenKM = tenKM;
                    km.DonGiaCu = donGiaCu;
                    km.DonGiaMoi = donGiaMoi;
                    km.KetThucKM = ketThucKM;
                    km.MoTa = moTa;
                    km.DiemNoiBat = dacDiemNoiBat;
                    km.DKSuDung = dkSuDung;
                    km.GioiThieuChiTiet = gioiThieuChiTiet;
                    km.DoanhNghiep = maDN;
                    km.HinhThucGiaoSP = hinhThucGiaoSP;
                    km.TinhTrang = tinhTrang;
                    km.HinhAnh = hinhAnh;
                    km.ClipQuangCao = km.ClipQuangCao;

                    db.AddToKHUYENMAIs(km);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Có lỗi xảy ra! Xin vui lòng kiểm tra và thử lại. " +
                       "Nếu vẫn còn lỗi xin liên hệ với admin.");
                }
            }
            return true;
        }
    }
}